package com.dayu.demo.controller;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.vecspace.data.model.EntityBO;
import com.vecspace.data.model.ListExBO;
import com.vecspace.data.model.FilterColumn;
import com.vecspace.dicttable.DictAll;
import com.vecspace.jsmodel.ZTreeNode;
import com.vecspace.msoffice.ExcelTemplate;
import com.vecspace.utils.Convert;
import com.vecspace.web.servlet.model.BaseController;
import com.vecspace.web.servlet.model.HController;
import com.vecspace.web.servlet.model.Param;
import com.vecspace.web.servlet.model.Path;
import com.vecspace.constant.AuthTag;
import com.vecspace.database.model.QueryConfig;
import com.dayu.demo.pojo.Employee;
import com.dayu.demo.pojo.EmployeeEx;
import com.dayu.demo.pojo.EmployeeStats;
import com.dayu.demo.service.EmployeeService;

/** 员工 Controller
 * 在Controller中进行与页面相关的处理，比如把数据转换为HTML格式等。
 * @template dayu-code-builder
 * @author user
 * @reviewer
 * @createTime 2021-11-25 16:23:48
 * @version 1.0
*/
@HController("employee")
public class EmployeeController extends BaseController {

    /**根据ID查询 
     * @return Employee
     * @throws Exception
     * @author user
     * @reviewer 
     * @createTime 2021-11-25 16:23:48
     */
    @Path("getEmployee")
    public Employee getEmployee(@Param("id") Long id) throws Exception {
        return employeeService.getEmployee(id);
    }
    
    @Path("getEmployeeBO")
    public EntityBO<Employee> getEmployeeBO(@Param("id") Long id) throws Exception {
        Employee employee = employeeService.getEmployee(id);
        EntityBO<Employee> entityBO = new EntityBO<Employee>(employee);
        entityBO.addDictCodeType("sex", DictAll.getItems(Employee.SexCode));
        return entityBO;
    }


    /** 插入或修改  
     * @param item    要保存的对象
     * @return void
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-25 16:23:48
     */
    @Path("saveEmployee")
    public EntityBO<Employee> saveEmployee(@Param("item") Employee item) throws Exception {
        //TODO：增加权限判断，是否可以新增、编辑
        if (item == null){
            return null;
        }
        /*** 对item对象进行校验，如必填项，格式，逻辑关系等 ***/
        Employee employee = employeeService.saveViewEmployee(item);
        return getEmployeeBO(employee.getId());
    }

    /**更新单个字段的值
     *
     * @param id 记录id
     * @param propName 属性名称
     * @param value 属性值
     * @return
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-25 16:23:48
    */
    @Path("updateProp")
    public void updateProp(@Param("id")Long id, @Param("propName")String propName, @Param("value")Object value) throws Exception {
        //TODO：增加权限判断，是否可以编辑
        employeeService.updateValueByProp(id, propName, value);
    }



    /**修改数据锁定状态
     * @param id 记录id，另-1表示对整表进行锁定
     * @param lockState 锁定状态，参考 DataLockState
     * @date 2021/11/17  20:45
     * @return void
     **/
    @Path("changeLockState")
	public void changeLockState(@Param("id")Long id, @Param("lockState")int lockState) throws Exception {
        int authTag = AuthTag.All;//从权限系统获取操作权限类型
        employeeService.changeLockState(id, lockState, authTag);
    }

    /** 根据ID删除 
     * @param id    主键值
     * @return void
     * @throws Exception
     * @author user
     * @reviewer
     * @createTime 2021-11-25 16:23:48
     */
    @Path("deleteEmployee")
    public void deleteEmployee(@Param("id") Long id) throws Exception {
        //TODO：增加权限判断，是否可以删除
        employeeService.deleteEmployee(id);
    }

    /**根据查询条件获取查询结果（分页查询），同时进行聚合统计查询
     * 
     * @param deptId 所属部门 
     * @param recursion 是否递归查询所有下级数据 
     * @param filters    查询条件（字段(属性)和值）
     * @param queryConfig  分页和排序参数
     * @return PageModel
     * @throws Exception
     * @author
     * @createTime 2021-10-05 23:16:31
     */
    @ExcelTemplate(templateFileName = "employee.xlsx")
    @Path("getEmployeeExListAndStats")
    public ListExBO<EmployeeEx, Void, EmployeeStats> getEmployeeExListAndStats(@Param("deptId")Long deptId, @Param("recursion")boolean recursion, @Param("filters") ArrayList<FilterColumn> filters,
          @Param("queryConfig") QueryConfig queryConfig) throws Exception {

        ListExBO<EmployeeEx, Void, EmployeeStats> employeeBOList = employeeService.getEmployeeExListAndStats(deptId, recursion, filters, queryConfig.getPageIndex(), queryConfig.getPageSize(), queryConfig.getSortName(), queryConfig.getSortOrder());

        employeeBOList.addDictCodeType("sex", DictAll.getItems(Employee.SexCode));

        return employeeBOList;
    }
    
    /************* property ********************/
    @Autowired
    private EmployeeService employeeService;


}
